$(function() {
	// 展示资源树
	$("#tt").tree({
		url:path+'/dic/tree',
		animate:true,
		lines:true,
		onClick:function(node){
			if($("#tt").tree("isLeaf",node.target)){
				clickNode(node);
			}
		},
		onLoadSuccess:function(node,data){
			$("#ey_grid").datagrid("options").url = path + "/item/page";
			$("#tt span.tree-file:eq(0)").parent("div").addClass("tree-node-selected");
			var node = $("#tt").tree("getSelected");
			clickNode(node);
		}
	});	

	// 右侧表格初始化
	$.table.init({
    	modalName:"字典项",
    	addUrl: path + "/item/add",
    	editUrl: path + "/item/edit/{id}",
    	saveUrl: path + "/item/save",
    	updateUrl: path + "/item/update",
    	deleteUrl: path + "/item/delete",
    	onDblClickRow:function(){
    		$.table.edit();
    	}
    });
});

/***************************************左侧树形功能操作---开始**************************/
function addDicM(){
	var node = $("#tt").tree("getSelected");
	// 不能在有字典项的字典下面添加新的字典
	$.post(path + "/item/check/" + node.id,function(result){
		if(result.state != web_status.SUCCESS){
			$.alert.error("该节点下存在字典项数据，不能操作！");
			return;
		}
		// 打开弹出
		$.modal.open({
			title:"添加字典",
			url:path + "/dic/add",
			onLoad:function(){
				$("#dic_parent_id").val(node.id);
				$("#dic_dic_code").textbox("readonly",false)
			},
			onSubmit:function(){
				submitDicM();
			}
		})
	});
}
function editDicM(){
	var node = $("#tt").tree("getSelected");
	// 打开弹出
	$.modal.open({
		title:"修改字典",
		url:path + "/dic/edit/" + node.id,
		onSubmit:function(){
			submitDicM("update");
		}
	})
}
function submitDicM(type){
	url = path + "/dic/save";
	if(type == "update"){
		url = path + '/dic/update';
	}
	$.form.submit({
		url:url,
		success:function(result){
			if (result.state == web_status.SUCCESS) {
				$.modal.close();
				var selected = $("#tt").tree("getSelected");
				if(type == "update"){ // 如果是修改
					$('#tt').tree('update',{
						target: selected.target,
						text:result.text,
						code:result.code
					});
				}else{
					$('#tt').tree('append',{
						parent: selected.target,
						data:[{
							id:result.id,
							text:result.text,
							code:result.code
						}]
					});
					var newNode = $("#tt").tree("find",result.id);
					$("#tt").tree("select",newNode.target)
					clickNode(newNode);
				}
			} else {
                $.alert.error(result.msg || "操作失败！");
            }
		}
	});
}
function deleteDicM(){
	var node = $("#tt").tree("getSelected");
	if(!$("#tt").tree("isLeaf",node.target)){
		$.alert.error("请先删除子节点！"); 
		return;
	}
	$.post(path+"/item/check/" + node.id,function(result){
		if(result.state != web_status.SUCCESS){
			$.alert.error("该节点下存在字典项数据，不能操作！");
			return;
		}
		$.alert.confirm("确认删除记录？", function() {
			$.post(path + "/dic/delete/" + node.id,function(result) {
				if (result.state != web_status.SUCCESS) {
					$.alert.error(result.msg || "操作失败！");
					return;
				}
				$('#tt').tree('reload');
			});
		});
	});
}
function clickNode(node){
	// 设置搜索条件的值
	$("#srh_dic_id").val(node.id);
	$("#srh_item_code").val("");
	// 设置页面中的描述 
	$("#dic_value").text(node.text);
	$("#dic_code").text(node.code);
	// 重新加载表格
	$.table.search();
}
/***************************************左侧树形功能操作---结束**************************/

/***************************************右侧列表功能操作---开始**************************/
function copyM(){
	var data = $("#ey_grid").datagrid("getData");
	var cv = {};
	$.each(data.rows,function(i,v){
		cv[v.item_code] = v.item_value;
	});
	$.alert.success(JSON.stringify(cv));
}
/***************************************右侧列表功能操作---结束**************************/